package com.ttxs.generator.output.java;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.ttxs.generator.engine.CustomFreemarker;
import com.ttxs.generator.output.Output;

import java.io.File;
import java.io.IOException;

public class Application implements Output {

    private static String firstUpperCase(String s) {
        return s.substring(0, 1).toUpperCase() + s.substring(1);
    }

    private static String toCamel(String artifactId) {
        return StringUtils.underlineToCamel(artifactId.replace("-", "_"));
    }

    @Override
    public void write(CustomFreemarker templateEngine) throws IOException {
        String parentPath = templateEngine.hasModule()
                ? templateEngine.getCustomParentPath()
                : templateEngine.getParentPath();
        String artifactId = templateEngine.formBean().getArtifactId();
        File application = new File(parentPath + File.separator +
                firstUpperCase(toCamel(artifactId)) + "Application.java");
        templateEngine.outputFile(application, templateEngine.customMap(), "templates/application.java.ftl");
    }
}
